/*
 * Copyright (C) 2011 PickleMan <admin.aegamesi@gmail.com>
 * 
 * This file is part of Jeie.
 * Jeie is free software and comes with ABSOLUTELY NO WARRANTY.
 * See LICENSE for details.
 */

package org.jeie.dialogs;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jeie.gui.IntegerTextField;
import org.jeie.messages.Messages;

public class DimensionDialog extends JDialog implements ActionListener {
	private static final long serialVersionUID = -3643159410803814805L;
	private JPanel myPanel = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JTextField wText = null;
	private JTextField hText = null;
	private Dimension dim = null;

	public Dimension getDimensions() {
		return dim;
	}

	public DimensionDialog(JFrame frame, String message) {
		super(frame, true);
		myPanel = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		getContentPane().add(myPanel);

		JLabel dimen = new JLabel(message, JLabel.CENTER);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;

		myPanel.add(dimen, c);
		c.gridwidth = 1;

		JLabel width = new JLabel(
			Messages.getString("Dialog.width_label"), JLabel.CENTER); //$NON-NLS-1$
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 1;
		myPanel.add(width, c);

		JLabel height = new JLabel(
			Messages.getString("Dialog.height_label"), JLabel.CENTER); //$NON-NLS-1$
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 1;
		myPanel.add(height, c);

		okButton = new JButton(Messages.getString("Dialog.okay_button")); //$NON-NLS-1$
		okButton.addActionListener(this);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 3;
		myPanel.add(okButton, c);

		cancelButton = new JButton(Messages.getString("Dialog.cancel_button")); //$NON-NLS-1$
		cancelButton.addActionListener(this);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 3;
		myPanel.add(cancelButton, c);

		wText = new IntegerTextField();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 2;
		myPanel.add(wText, c);

		hText = new IntegerTextField();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 2;
		myPanel.add(hText, c);

		pack();
		setLocationRelativeTo(frame);
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == okButton
			&& !wText.getText().isEmpty()
			&& !hText.getText().isEmpty()) {
			dim = new Dimension(
				Integer.parseInt(wText.getText()),
				Integer.parseInt(hText.getText()));
			setVisible(false);
		}
		if (e.getSource() == cancelButton) {
			dim = null;
			setVisible(false);
		}
	}
}
